<?php

namespace app\command;

use app\common\lib\BaseCommand;
use app\common\lib\Common;
use app\logic\cron\DouyinLogic;
use bootstrap\DBListen;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Webman\Config;
use Webman\Event\Event;


class CronDouyin extends Command
{
    protected static $defaultName = 'cron:douyin';
    protected static $defaultDescription = 'cron douyin';

    /**
     * @return void
     */
    protected function configure()
    {
        //发布执行结束
        Event::dispatch('console.start', []);

        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');

        Common::initTaskConfig();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $cmd = 'php webman '.self::$defaultName.($name?' '.$name:'');
        try{
            $startTime = round(microtime(true),4);

            $result = [];
            switch ($name){
                case 'getTaskDetailByIds':
                    $result = $this->getTaskDetailByIds();
                    break;
                case 'sysncNovelByTaskDetail':
                    $result = $this->sysncNovelByTaskDetail();
                    break;
                case 'sysncDailyIncomeData':
                    $result = $this->sysncDailyIncomeData();
                    break;
                case 'sysncAgentAndUidBind':
                    $result = $this->sysncAgentAndUidBind();
                    break;
            }

            $ttls =  round(round(microtime(true),4) - $startTime,4);
            //发布执行结束
            Event::dispatch('console.terminate', [$cmd,$result,$ttls]);

            return self::SUCCESS;
        }catch (\Throwable $e){
            //var_dump($e->getMessage());
            Common::consoleExecptionReport($cmd,$e);
            return self::FAILURE;
        }

    }

    /**
     * 查询推广任务id和任务详情数据
     * @return array
     */
    public function getTaskDetailByIds()
    {
        $result = DouyinLogic::getInstance()->getTaskDetailByIds();

        return $result;
    }

    /**
     * 通过推广任务数据同步数据到小说
     * @return array
     */
    public function sysncNovelByTaskDetail()
    {
        $result = DouyinLogic::getInstance()->sysncNovelByTaskDetail();

        return $result;
    }

    /**
     * 获取每天用户收益数据
     * @return string
     * @throws \Exception
     */
    public function sysncDailyIncomeData()
    {
        $result = DouyinLogic::getInstance()->sysncDailyIncomeData();

        return $result;
    }

    /**
     * 查询团长和用户绑定关系
     * @return string
     * @throws \Exception
     */
    public function sysncAgentAndUidBind()
    {
        $result = DouyinLogic::getInstance()->sysncAgentAndUidBind();

        return $result;
    }

}
